﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Areas/Admin/Views/Shared/_LayoutList.cshtml";
}

<div class="admin-main">
    <blockquote class="layui-elem-quote p10">
        <form id="formSearch" class="layui-form">
            <div class="layui-form-text">
                <label class="layui-form-label">产品类目：</label>
                <div class="layui-input-inline" style="width:10%">
                    @Html.CategoryHtml(ViewData["BaseCategory"],"NZ")
                </div>
                <div class="layui-input-inline">
                    @Html.SearchBtnHtml("查询")
                </div>
                <div class="layui-input-inline" style="width:20%;float:right">
                    <label class="layui-form-label">运费模板:</label>
                    <div class="layui-input-inline" style="width:140px">
                        @Html.ShipHtml(ViewData["ShipTemplate"], "")
                    </div>
                    <div class="layui-input-inline" style="margin-left:15px">
                        <a class="layui-btn" lay-filter="submitForm" id="saveForm">刊登</a>
                    </div>
                </div>

                <div class="layui-input-inline" style="width:12%;float:right">
                    <label class="layui-form-label">促销日期:</label>
                    <div class="layui-input-inline" style="width:90px">
                        <select id="selDuration">
                            <option value="" selected="selected">请选择</option>
                            <option value="1"> 1年 </option>
                            <option value="2"> 2年 </option>
                            <option value="5"> 5年 </option>
                            <option value="10"> 10年 </option>
                        </select>
                    </div>
                </div>
          
                <div style="float:right;">
                    @Html.TopToolBarHtml(ViewData["ActionFormRightTop"])
                </div>
            </div>
        </form>
    </blockquote>
    <blockquote class="layui-elem-quote p10">
        <form id="formEdit" class="layui-form">
            <div class="layui-field-box">
                <table id="defaultTable" lay-filter="defaultruv"></table>
                <script type="text/html" id="bar">
                    @Html.ToolBarHtml(ViewData["ActionList"])
                </script>
            </div>
        </form>
    </blockquote>
</div>
<script type="text/javascript">
    layui.use(['table', 'common', 'form','tablePlug', 'optimizeSelectOption'], function () {
        var table = layui.table,
            form = layui.form,
            common = layui.common,
             $ = layui.$;
    

        //表格
        table.render({
            id: 'defaultReload'
           , elem: '#defaultTable'
           , height: 665
           , url: '/DataSrv/PlatForm/AmazonList' //数据接口
           , page: true //开启分页
           , cols: [[ //表头
                     { checkbox: true, fixed: false }
                     , { title: '图片', field: 'ImageUrl',  width: 70, templet: '<div><img prodId="{{d.Id}}" style="width: 50px; height:50px; display: block;" src="{{d.ImageUrl}}"/></div>', align: 'center' }
                     , { field: 'ProductSku', width: 130, title: 'SkuId', align: 'center' }
                     , { field: 'Title', title: '标题', width: 600, align: 'center', templet: '<div><input type="text" lay-verify="required" value="{{d.Title}}" lay-vertype="tips" id="prop_1" class="layui-input" autocomplete="off"></div>' }
                     , { field: 'UpdateOn', title: '更新时间', width: 180, templet: '<div>{{showDate(d.UpdateOn)}}</div>', align: 'center' }
                       @Html.TabaleHeadHtml(ViewData["TableHead"])          
                     , { field: '', title: '操作', width: 250, toolbar: "#bar" }
           ]]
        });
        var active = {
            reload: function () {
                $('#ajax-loader').fadeOut(); //todo：加载滚动条
                //执行重载
                table.reload('defaultReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                  , where: urlToJson(encodeURIComponent($("#formSearch").serialize()))
                });
            }
        };
        //服务器排序
        table.on('sort(defaultruv)', function (obj) {
            //尽管我们的 table 自带排序功能，但并没有请求服务端。
            //有些时候，你可能需要根据当前排序的字段，重新向服务端发送请求，如：
            table.reload('defaultReload', {
                initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。 layui 2.1.1 新增参数
              , where: { //请求参数
                  field: obj.field //排序字段
                , order: obj.type //排序方式
              }
            });
        });
        $('#btnSearch').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //$('#saveForm').on('click', function () {
           
        //});

        $("#saveForm").hover(function() {
            //openMsg();
        }, function() {
        });
        function openMsg() {
            subtips = layer.tips('提示信息', '#sub',{tips:[1,'#ff6700'],time: 30000});
        } 
        
        //add
        $('#btnAdd').on('click', function () {
            common.openTop({
                title: '操作添加', w: '670px', h: '500px', content: '/Datasrv/Product/Add/'
            });
        });
        //监听工具条
        table.on('tool(defaultruv)', function (obj) {
            var data = obj.data;
           
            switch (obj.event) {
                case "publish":
                
                    var duration = $("#selDuration").val();
                    var shipTemplateId = $("#selShipCode").val();
                    if (duration == null || duration == '') {
                        layer.msg('请选择促销日期', {
                            icon: 2,
                            time: 1000
                        });
                    }
                   else if (shipTemplateId == null || shipTemplateId == '') {
                        layer.msg('请选择运费模板', {
                            icon: 2,
                            time: 1000
                        });                       
                    } else {
                        layer.load(); //loading
                        $.ajax({
                            url: "/Datasrv/PlatForm/PublishToAmazon",
                            type: "POST",
                            data: { "Id": data.Id, "PromotionDuration": duration, "ShipTemplateId": shipTemplateId },
                            dataType: "json",
                            success: function (data) {
                                layer.closeAll('loading'); //关闭loading
                                if (data.state == "success") {
                                    common.msgSuccess("提交成功");
                                } else {
                                    common.msgError("提交失败");
                                }
                            }
                        });
                    
                    }
                    break;
                case "detail":
                 
                    break;
                case "del":
                    layer.confirm('确定要删除吗？', function (index) {
                        $.ajax({
                            url: "/Datasrv/PlatForm/AmazonDelete",
                            type: "POST",
                            data: { "Id": data.Id },
                            dataType: "json",
                            success: function (data) {
                                if (data.state == "success") {
                                    obj.del();//删除这一行
                                    common.msgSuccess("删除成功");
                                } else {
                                    common.msgError("删除失败");
                                }
                            }
                        });
                    });
                    break;
                case "edit":
                    common.openTop({
                        detail: true,
                        title: '编辑商品', w: '1500px', h: '750px', content: '/Datasrv/PlatForm/AmazonEdit/' + data.Id, clickOK: function (index) {
                            common.close(index);
                        }
                    });
                    break;

                    //window.open('/Datasrv/Product/Edit/' + data.Id, "_blank");
                    //location.href = '/Datasrv/Product/Edit/' + data.Id;


                default: break;
               

            }
            return false;
        });
    });
</script>


